class CustomProductService
  include Serviceable
  include StatusObject

  def initialize(cart, store, product_params, cart_item_params)
    @cart = cart
    @store = store
    @product_params = product_params
    @cart_item_params = cart_item_params
  end

  def call
    ActiveRecord::Base.transaction do
      @product = @store.products.create!(@product_params.except(:images_attributes))
      status = ImageService.call(@product_params[:images_attributes], @product)
      if !status.success?
        @product.delete
        Status.new(success: false, notice: status.notice)
      else
        @cart_item = @cart.items.create!(@cart_item_params.merge(store_product_id: @product.id))
        Status.new(success: true, notice: status.notice, obj: @cart_item, obj_id: @product.id)
      end
    end #end transaction
  rescue => e
    Rails.logger.info e.message
    Status.new(success: false, notice: e.message)
  end
end
